
from utils.utils import *
import allure
from faker import Faker
faker = Faker(locale='zh_cn')
name=faker.name()

@allure.story("查询用户分组")
def test_group():

    reset_token()

    Query_user_groups = send_requests(
        "查询用户分组",
        "get",
        "/adminapi/user/user_group/list?page=1&limit=1",
    ).\
    validate().\
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "success")
    #添加step
    Query_user_groups_step = step_obj(Query_user_groups)

    res = get_last_response([Query_user_groups_step,])


@allure.story("新增用户分组")
def test_add_group():
    reset_token()

    # 创建请求步骤
    add_step = send_requests(
        "新增用户分组",
        "post",
        "/adminapi/user/user_group/save.html",
        {"group_name": name}  # 使用 name 变量
    ).validate(). \
        assert_equal("status_code", 200). \
        assert_equal('headers."Content-Type"', "application/json; charset=utf-8"). \
        assert_equal("body.status", 200). \
        assert_equal("body.msg", "提交成功"). \
        assert_equal("body.code", "100017")

    # 执行请求
    add_step_step = step_obj(add_step)
    response = get_last_response([add_step_step])


@allure.story("修改用户分组")
def test_modify_group():
    reset_token()
    get_user_group = send_requests('查询用户分组','get','/adminapi/user/user_group/list?page=1&limit=15').\
    validate(). \
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "success")
    get_user_group_step = step_obj(get_user_group)
    res = get_last_response([get_user_group_step,])
    for i in res['res_body']['data']['list']:
        if i['group_name'] == name:
            id = i['id']
    #修改用户分组
    update_user_group = send_requests(
        '修改用户分组',
        'post',
        "/adminapi/user/user_group/save.html",
        {"id":str(id), "group_name": f"{name}"}

    ).\
    validate().\
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "提交成功").\
    assert_equal("body.code", "100017")
    update_user_group_step = step_obj(update_user_group)
    res = get_last_response([update_user_group_step,])

@allure.story("删除用户组")
def test_delGroup():
    reset_token()
    get_user_group = send_requests('查询用户分组', 'get', '/adminapi/user/user_group/list?page=1&limit=15'). \
    validate(). \
    assert_equal("status_code", 200). \
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8"). \
    assert_equal("body.status", 200). \
    assert_equal("body.msg", "success")
    get_user_group_step = step_obj(get_user_group)
    res = get_last_response([get_user_group_step, ])
    for i in res['res_body']['data']['list']:
        if i['group_name'] == name:
            id = i['id']
    #删除
    delete_user_group = send_requests(
        "删除用户分组",
        'delete',
        f"/adminapi/user/user_group/del/{id}"
    ).\
    validate().\
    assert_equal("status_code", 200).\
    assert_equal('headers."Content-Type"', "application/json; charset=utf-8").\
    assert_equal("body.status", 200).\
    assert_equal("body.msg", "删除成功!")
    delete_user_group_step = step_obj(delete_user_group)
    res = get_last_response([delete_user_group_step,])







